<?php

namespace App\Models;

class Activitys extends Base
{
    protected $table = 'activitys';

    const CHARGE_1 = 1;
    const CHARGE_2 = 2;
    const CHARGE_TEXT = [
        self::CHARGE_1 => '<span class="el-tag  el-tag--danger">收费</span>',
        self::CHARGE_2 => '<span class="el-tag">免费</span>',
    ];

    const SOURCE_1 = 1;
    const SOURCE_2 = 2;
    const SOURCE_TEXT = [
        self::SOURCE_1 => '<span class="el-tag  el-tag--danger">平台</span>',
        self::SOURCE_2 => '<span class="el-tag">供应商</span>',
    ];

    const HOT_1 = 1;
    const HOT_2 = 2;
    const HOT_TEXT = [
        self::HOT_1 => '<span class="el-tag  el-tag--danger">否</span>',
        self::HOT_2 => '<span class="el-tag">是</span>',
    ];

    const STATUS_1 = 1;
    const STATUS_2 = 2;
    const STATUS_3 = 3;
    const STATUS_4 = 4;
    const STATUS_TEXT = [
        self::STATUS_1 => '<span class="el-tag">正常</span>',
        self::STATUS_2 => '<span class="el-tag el-tag--danger">待审核</span>',
        self::STATUS_3 => '<span class="el-tag el-tag--danger">未通过</span>',
        self::STATUS_4 => '<span class="el-tag el-tag--danger">已取消</span>',
    ];

    protected $fillable = [
        'id','user_id','sponsor','thumb','title','cate_pid','cate_id','content','cost','place','number','limit','view','collect','link_name','area',
        'link_phone','link_department','link_position','enroll_at','start_at','end_at','charge','source','hot','status','created_at','updated_at'
    ];

    public function cate_pid(){//一级分类
        return $this->belongsTo('App\Models\ActivityCategorys', 'cate_pid', 'id');
    }
    public function cate_id(){//二级分类
        return $this->belongsTo('App\Models\ActivityCategorys', 'cate_id', 'id');
    }

    /**
     * 获取列表
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @param int $limit 分页数量
     * @return mixed
     */
    protected function getList($params = [], $fields = ['*'], $limit = 10){
        $lists = $this->select($fields)
            ->where(function ($query) use ($params) {
                if(isset($params['kwd']) && $params['kwd']) {
                    $query ->where('title','like','%' . $params['kwd'] . '%');
                }
                if(isset($params['charge']) && $params['charge']) {
                    $query ->where('charge',$params['charge']);
                }
                if(isset($params['source']) && $params['source']) {
                    $query ->where('source',$params['source']);
                }
                if(isset($params['status']) && $params['status']) {
                    $query ->where('status',$params['status']);
                }
                if(isset($params['cid']) && $params['cid']) {
                    $query ->where('cate_id',$params['cid'])->orwhere('cate_pid',$params['cid']);
                }
                if(isset($params['start']) && $params['start']) {
                    $query ->whereDate('created_at','>=',$params['start']);
                }
                if(isset($params['end']) && $params['end']) {
                    $query ->whereDate('created_at','<=',$params['end']);
                }
            })->where('status','<>',-1)
            ->with(['cate_id' => function ($query) {
                $query->select(['id','name'])->get();
            }])->with(['cate_pid' => function ($query) {
                $query->select(['id','name'])->get();
            }])->when($params, function ($query) use($params){
                if(isset($params['sort']) && $params['sort']) {
                    return $query->orderBy('view',$params['sort']);
                }
            })->orderBy('created_at', 'desc')
            ->paginate($limit);

        $this->stateToText($lists,[
            'status'=> self::STATUS_TEXT,
            'charge'   => self::CHARGE_TEXT,
            'hot'   => self::HOT_TEXT,
        ]);
        return $lists;
    }

//    /**
//     * 接口获取列表
//     * @param array $params 条件参数
//     * @param array $fields 查询的字段
//     * @return mixed
//     */
//    protected function apiList($params = [], $limit = 10, $fields = ['*'], $sort=''){
//        $params['status'] = 1;
//        $lists = $this->select($fields)
//            ->where(function ($query) use ($params) {
//                if(isset($params['status']) && $params['status']) {
//                    $query ->where('status',$params['status']);
//                }
//                if(isset($params['hot']) && $params['hot']) {
//                    $query ->where('hot',$params['hot']);
//                }
//                if(isset($params['cid']) && $params['cid']) {
//                    $query ->where('cate_id',$params['cid'])->orwhere('cate_pid',$params['cid']);
//                }
//            })->where('status','<>',-1)
//            ->with(['cate_pid' => function ($query) {
//                $query->select(['id','name'])->get();
//            }])->with(['cate_id' => function ($query) {
//                $query->select(['id','name'])->get();
//            }])->orderBy($sort,'desc')->limit($limit)->get();
//
//        return $lists;
//    }

    /**
     * 所有数据
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @param int $limit 分页数量
     * @return mixed
     */
    protected function allLists($params = [], $fields = ['*'], $page = '', $limit = 10, $sort = 'created_at', $orderby = 'desc'){
        if(isset($page)) {
            $lists = $this->select($fields)
                ->where(function ($query) use ($params) {
                    if(isset($params['id']) && $params['id']) {
                        $query ->whereIn('id',$params['id']);
                    }
                    if(isset($params['user_id']) && $params['user_id']) {
                        $query ->where('user_id',$params['user_id']);
                    }
                    if(isset($params['status']) && $params['status']) {
                        $query ->where('status',$params['status']);
                    }
                    if(isset($params['hot']) && $params['hot']) {
                        $query ->where('hot',$params['hot']);
                    }
                    if(isset($params['area']) && $params['area']) {
                        $query ->where('area',$params['area']);
                    }
                    if(isset($params['charge']) && $params['charge']) {
                        $query ->where('charge',$params['charge']);
                    }
                    if(isset($params['cid']) && $params['cid']) {
                        $query ->where('cate_pid',$params['cid']);
                    }
                    if(isset($params['cid_id']) && $params['cid_id']) {
                        $query ->whereIn('cate_id',$params['cid_id']);
                    }
                    if(isset($params['enroll_at']) && $params['enroll_at']) {
                        $query ->whereDate('enroll_at','>=',$params['enroll_at']);
                    }
                    if(isset($params['sign_at']) && $params['sign_at']) {
                        $query ->whereDate('start_at','>',$params['sign_at']);
                    }
                    if(isset($params['start_at']) && $params['start_at']) {
                        $query ->whereDate('start_at','<=',$params['start_at']);
                    }
                    if(isset($params['now_at']) && $params['now_at']) {
                        $query ->whereDate('enroll_at','<',$params['now_at'])->whereDate('start_at','>',$params['now_at']);
                    }
                    if(isset($params['keyword']) && $params['keyword']) {
                        $keyword = $params['keyword'];
                        $query ->where("title", "like", "%$keyword%")->orWhere("sponsor", "like", "%$keyword%");
                    }
                })->where('status','<>',-1)
                ->with(['cate_pid' => function ($query) {
                    $query->select(['id','name'])->get();
                }])->with(['cate_id' => function ($query) {
                    $query->select(['id','name'])->get();
                }])->orderBy($sort, $orderby)->paginate($limit, null, null, $page);
        }else {
            $lists = $this->select($fields)
                ->where(function ($query) use ($params) {
                    if(isset($params['id']) && $params['id']) {
                        $query ->whereIn('id',$params['id']);
                    }
                    if(isset($params['user_id']) && $params['user_id']) {
                        $query ->where('user_id',$params['user_id']);
                    }
                    if(isset($params['status']) && $params['status']) {
                        $query ->where('status',$params['status']);
                    }
                    if(isset($params['hot']) && $params['hot']) {
                        $query ->where('hot',$params['hot']);
                    }
                    if(isset($params['area']) && $params['area']) {
                        $query ->where('area',$params['area']);
                    }
                    if(isset($params['charge']) && $params['charge']) {
                        $query ->where('charge',$params['charge']);
                    }
                    if(isset($params['cid']) && $params['cid']) {
                        $query ->where('cate_pid',$params['cid']);
                    }
                    if(isset($params['cid_id']) && $params['cid_id']) {
                        $query ->whereIn('cate_id',$params['cid_id']);
                    }
                    if(isset($params['enroll_at']) && $params['enroll_at']) {
                        $query ->whereDate('enroll_at','>=',$params['enroll_at']);
                    }
                    if(isset($params['start_at']) && $params['start_at']) {
                        $query ->whereDate('start_at','<=',$params['start_at']);
                    }
                    if(isset($params['now_at']) && $params['now_at']) {
                        $query ->whereDate('enroll_at','<',$params['now_at'])->whereDate('start_at','>',$params['now_at']);
                    }
                    if(isset($params['keyword']) && $params['keyword']) {
                        $keyword = $params['keyword'];
                        $query ->where("title", "like", "%$keyword%")->orWhere("sponsor", "like", "%$keyword%");
                    }
                })->where('status','<>',-1)
                ->with(['cate_pid' => function ($query) {
                    $query->select(['id','name'])->get();
                }])->with(['cate_id' => function ($query) {
                    $query->select(['id','name'])->get();
                }])->orderBy($sort,'desc')->limit($limit)->get();
        }
        return $lists;
    }

    /**
     * 根据id查询一条信息
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @return mixed
     */
    protected function details($id, $fields = ['*']){
        return $this->select($fields)->where($id)->where('status','<>',-1)
            ->with(['cate_pid' => function ($query) {
            $query->select(['id','name'])->get();
        }])->with(['cate_id' => function ($query) {
            $query->select(['id','name'])->get();
        }])->first();
    }

    /**
     * 获取条数
     * @param Request $request
     * @return bool
     */
    protected function getCounts($params = [], $fields = ['*']){
        $counts = $this->where(function ($query) use ($params) {
                if(isset($params['user_id']) && $params['user_id']) {
                    $query ->where('user_id',$params['user_id']);
                }
                if(isset($params['enroll_at']) && $params['enroll_at']) {
                    $query ->whereDate('enroll_at','>=',$params['enroll_at']);
                }
                if(isset($params['start_at']) && $params['start_at']) {
                    $query ->whereDate('start_at','<=',$params['start_at']);
                }
            })->where('status','<>',-1)->count();
        return $counts;
    }

}
